package com.glorysoft.sunnypda.ui.reportfault

import android.database.Observable
import com.glorysoft.sunnypda.base.BaseViewModel
import com.glorysoft.sunnypda.base.Constant
import com.glorysoft.sunnypda.entity.RepairInfoEntity
import com.glorysoft.sunnypda.entity.ui.CallObserveBean
import com.glorysoft.sunnypda.model.EqpRepository
import com.glorysoft.sunnypda.model.RepairRepository
import com.google.gson.JsonObject
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DateUtils

/**
 * FileName: ReportFaultViewModel
 * Author: XJH
 * Date: 2022/1/7 15:58
 * Description:
 * History:
 */
class ReportFaultViewModel : BaseViewModel(){

    val repairRepository : RepairRepository by lazy { RepairRepository() }
    var mRepairFaultEntity : RepairInfoEntity = RepairInfoEntity()
    val eqpRepository : EqpRepository = EqpRepository()
    val priorityList = arrayListOf("低","中","高")
    val typeList = ArrayList<String>()

    fun initRepairForEqp(eqp : String){
        val map = JsonObject()
        map.addProperty("EQPID",eqp)
        launchOnlyResult({
            val eqpInfo = eqpRepository.getEqpInfo(map)
            getTypeInfo(eqp)
            defUI.callObserve.value = CallObserveBean(0,eqpInfo)
        })
    }

    fun reportFault(eqp : String){
        val map = JsonObject()
        map.addProperty("RFR_ID",mRepairFaultEntity.RFR_ID)
        map.addProperty("RFR_OP",Constant.CURRENT_USER)
        map.addProperty("EQP_ID",eqp)
        map.addProperty("EQP_TYPE",mRepairFaultEntity.EQP_TYPE)
        map.addProperty("RFR_TYPE",mRepairFaultEntity.RFR_TYPE)
        map.addProperty("LOT_ID",mRepairFaultEntity.LOT_ID)
        map.addProperty("PRIORITY",mRepairFaultEntity.PRIORITY)
        map.addProperty("SHIFT",mRepairFaultEntity.SHIFT)
        map.addProperty("RFR_CURR_STATE",mRepairFaultEntity.RFR_CURR_STATE)
//        map.addProperty("RFR_CREATE_TIME",FreeApi_DateUtils.getNowStringDate("yyyy-MM-dd HH:mm:ss"))
        map.addProperty("REMARK",mRepairFaultEntity.REMARK)
        map.addProperty("RPR_INPUT",mRepairFaultEntity.RPR_INPUT)
        map.addProperty("CREATE_TIME",FreeApi_DateUtils.getNowStringDate("yyyy-MM-dd HH:mm:ss"))
        launchOnlyResult({
            repairRepository.reportFault(map)
            defUI.startActivity.call()
        })
    }

    fun getTypeInfo(qepID:String){
        val map = JsonObject()
        map.addProperty("EQPID",qepID)
        launchOnlyResult({
            val typeListInfo = repairRepository.getTypeInfo(map)
            typeListInfo.forEach{
                typeList.add(it.faulT_TYPE)
            }
        })
    }

}